package xhe.lib.io;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.google.gson.GsonBuilder;

public class Gson {

	/**
	 * Gson 只转换添加注解的字段
	 */
	public static com.google.gson.Gson getExcludeGson() {
		return GsonFactory.getExcludeGson();
	}

	/**
	 * 获取普通Gson对象
	 */
	public static com.google.gson.Gson getGson() {
		return GsonFactory.getGson();
	}

	/**
	 * 根据key获取json
	 */
//	public static Object getJsonByKey(String json, String key) {
//		try {
//			return JSONObject.fromObject(json).get(key);
//		} catch (Exception e) {
//			return "";
//		}
//	}

	/**
	 * 根据key获取json
	 */
	public static String getJsonByKeySp(String json, String key) {
		String strValue = "";
		try {
			if (key.equals("DATA")) {
				int keyIndex = json.indexOf(key);
				String strJson = json.substring(keyIndex);

				int keyIndex1 = strJson.indexOf("\"");
				String strJson1 = strJson.substring(keyIndex1);

				int keyIndex2 = strJson1.indexOf("\"");
				String strJson2 = strJson1.substring(keyIndex2 + 2);

				int keyIndex3 = strJson2.indexOf("]");
				String strJson3 = strJson2.substring(0, keyIndex3 + 1);

				strValue = strJson3;
			} else {
				int keyIndex = json.indexOf(key);
				String strJson = json.substring(keyIndex);

				int keyIndex1 = strJson.indexOf("\"");
				String strJson1 = strJson.substring(keyIndex1);

				int keyIndex2 = strJson1.indexOf("\"");
				String strJson2 = strJson1.substring(keyIndex2 + 3);

				int keyIndex3 = strJson2.indexOf("\"");
				String strJson3 = strJson2.substring(0, keyIndex3);

				strValue = strJson3;
			}
		} catch (Exception e) {
			strValue = "";
		}
		return strValue;
	}

//	/**
//	 * @param jsonStr json字符串
//	 * @return 使用json库解析json
//	 */
//	public static JSONObject getJsonObject(String jsonStr) {
//		return JSONObject.fromObject(jsonStr);
//	}

	private static class GsonFactory {
		public static com.google.gson.Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		// public static Gson gson = new
		// GsonBuilder().setDateFormat(DateUtil.DATE_FORMAT1).create();
		public static GsonBuilder builder = new GsonBuilder();

		private static com.google.gson.Gson getGson() {
			return gson;
		}

		private static com.google.gson.Gson getExcludeGson() {
			builder.excludeFieldsWithoutExposeAnnotation();
			return builder.create();
		}

	}

	public static String analysis(String json, String key) {
		String result = "";
		try {
			boolean b = isJson(json);
			if (b) {
				System.out.println("analysiskey:" + key);
				JSONObject jsonObject = JSONObject.parseObject(json, Feature.OrderedField);
				Object value = jsonObject.get(key);
				result = value.toString();
				System.out.println("analysis:" + result);
			} else {
				result = "";
			}
		} catch (Exception e) {
			result = "";
			System.out.println("analysisException:" + e.getMessage());
		}
		return result;
	}

	/**
	 * 判断传入的字符串是否是json格式
	 * 
	 * @param content
	 * @return true:yes false: no
	 */
	public static boolean isJson(String content) {
		try {
			JSONObject.parseObject(content);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 判断传入的字符串是否是json格式
	 * 
	 * @param content
	 * @return true:yes false: no
	 */
	public static boolean isJsonFrom(String content, String Keys) {
		boolean result = true;
		try {
			String[] siKeys = Keys.split(",");
			for (int i = 0; i < siKeys.length; i++) {
				if (!content.contains(siKeys[i])) {
					result = false;
				}
			}
		} catch (Exception e) {
			System.out.println("-----isJsonFrom-catch-----" + content);
			result = false;
		}

		return result;
	}
}